package cn.com.tianpeidong.restaurantadmin.project.admin.pos.service.impl;

import cn.com.tianpeidong.restaurantadmin.common.util.date.DateUtils;
import cn.com.tianpeidong.restaurantadmin.project.admin.desk.constant.DeskStatus;
import cn.com.tianpeidong.restaurantadmin.project.admin.desk.mapper.DeskMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.entity.Sale;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.entity.SaleLineItem;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.mapper.PosMapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.service.IPosService;
import cn.com.tianpeidong.restaurantadmin.project.admin.pos.util.PosCacheManager;
import com.alibaba.fastjson.JSONArray;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

import static cn.com.tianpeidong.restaurantadmin.project.admin.pos.constant.PosConstant.OPERATING;
import static cn.com.tianpeidong.restaurantadmin.project.admin.pos.constant.PosConstant.UNPAY;

/**
 * @author tianpeidong
 * @version 创建时间: 19-6-1 下午1:29
 */
@Service
public class PosServiceImpl implements IPosService {

    @Autowired
    private PosCacheManager posCacheManager;

    @Autowired
    private PosMapper posMapper;

    @Autowired
    private DeskMapper deskMapper;

    @Override
    public Sale newSale(Sale sale) {
        Sale newSale = new Sale();
        newSale.setDeskId(sale.getDeskId());
        newSale.setDeskCode(sale.getDeskCode());
        newSale.setPeopleNum(sale.getPeopleNum());
        posCacheManager.updateSale(newSale);
        deskMapper.updateStatusById(DeskStatus.BUSY.getCode(), sale.getDeskId());
        return newSale;
    }

    @Override
    public Sale add(@NonNull Long deskId, @NonNull SaleLineItem saleLineItem) {
        Sale curSale = getSale(deskId);
        if (curSale.getStatus().equals(OPERATING)) {
            curSale.addSaleLineItem(saleLineItem);
            posCacheManager.updateSale(curSale);
        }
        return curSale;
    }

    @Override
    public Sale getSale(@NonNull Long deskId) {
        return posCacheManager.getSale(deskId);
    }

    @Override
    public Sale removeDishes(@NonNull Long deskId, @NonNull SaleLineItem saleLineItem) {
        Sale curSale = getSale(deskId);
        if (curSale.getStatus().equals(OPERATING)) {
            curSale.removeSaleLineItem(saleLineItem);
            posCacheManager.updateSale(curSale);
        }
        return curSale;
    }

    @Override
    public BigDecimal calPrice(@NonNull Long deskId) {
        return getSale(deskId).getTotals();
    }

    @Override
    public void dropSale(@NonNull Long deskId) {
        posCacheManager.dropSale(deskId);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Sale makeSale(@NonNull Long deskId) {
        Sale curSale = getSale(deskId);
        if (curSale.getStatus().equals(OPERATING)) {
            curSale.setDate(DateUtils.dateTimeNow("yyyy-MM-dd"));
            curSale.setCreatedTime(new Date());
            curSale.setStatus(UNPAY);
            posMapper.makeSale(curSale);
            curSale.getSaleLineItems().forEach((x) -> {x.setSaleId(curSale.getId());x.setDate(DateUtils.dateTimeNow("yyyy-MM-dd"));});
            posMapper.makeSaleItem(curSale.getSaleLineItems());
            posCacheManager.dropSale(deskId);
        }
        return curSale;
    }

}
